# -*- coding: utf-8 -*-

###########################################################################
#
#   Copyright (c) 2020  Bresee, Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
###########################################################################

from flask import request


def get_response_json(name):
    try:
        return request.json[name] or None
    except:
        return None


def get_response_args(name):
    try:
        return request.args.get(name) or None
    except:
        return None


def get_response_form(name):
    try:
        return request.form.get(name, type=str, default=None)
    except:
        return None


def get_response_values(name):
    try:
        return request.values.get(name) or None
    except:
        return None


def get_request_str(name, defaultValue=''):
    value = get_response_json(name)
    if value is None:
        value = get_response_args(name)
    if value is None:
        value = get_response_form(name)
    if value is None:
        value = get_response_values(name)
    if value is None:
        value = defaultValue
    return value
